package jnpf.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.Pagination;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.exception.WxErrorException;
import jnpf.model.mpuser.MpUserListVO;
import jnpf.model.mpuser.MpUserModel;
import jnpf.model.mpuser.MpUserRemarkForm;
import jnpf.service.MpUserService;
import jnpf.util.JsonUtil;
import jnpf.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 公众号用户
 */
@Api(tags = "公众号用户", value = "MPUser")
@RestController
@RequestMapping("/api/third/WeChat/MPUser")
public class MpUserController {

    @Autowired
    private MpUserService mpUserService;

    /**
     * 获取标签用户列表
     *
     * @param type       用户：user-已关注、unblack-黑名单、标签用户列表
     * @param pagination
     * @return
     */
    @ApiOperation("用户列表")
    @GetMapping("/{type}")
    public ActionResult<PageListVO<MpUserListVO>> getList(Pagination pagination, @PathVariable("type") String type) throws WxErrorException {
        List<MpUserModel> data;
        if ("user".equals(type)) {
            data = mpUserService.getList(pagination);
        } else if ("unblack".equals(type)) {
            data = mpUserService.getBlackList(pagination);
        } else {
            data = mpUserService.getListByTagId(pagination, type);
        }
        if (!StringUtil.isEmpty(pagination.getKeyword())) {
            data = data.stream().filter(t -> t.getNickname().contains(pagination.getKeyword())).collect(Collectors.toList());
        }
        PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
        List<MpUserListVO> listVO = JsonUtil.getJsonToList(data, MpUserListVO.class);
        return ActionResult.page(listVO, paginationVO);
    }

    /**
     * 修改关注者备注信息
     *
     * @return
     */
    @ApiOperation("修改备注")
    @PutMapping("/{openId}/Actions/Remark")
    public ActionResult updateRemark(@PathVariable("openId") String openId, @RequestBody MpUserRemarkForm mpUserRemarkForm) throws WxErrorException {
        MpUserModel userModel = new MpUserModel();
        userModel.setOpenid(openId);
        userModel.setRemark(mpUserRemarkForm.getRemark());
        boolean flag = mpUserService.updateRemark(userModel);
        if (flag == false) {
            return ActionResult.fail("更新失败，数据不存在");
        }
        return ActionResult.success("更新成功");
    }

    /**
     * 加入黑名单
     *
     * @param openId 关注者Id
     * @return
     */
    @ApiOperation("加入黑名单")
    @PostMapping("/{openId}/Actions/BlackList")
    public ActionResult addBatchBlack(@PathVariable("openId") String openId) throws WxErrorException {
        boolean flag = mpUserService.addBatchBlack(openId);
        if (flag == false) {
            return ActionResult.fail("操作失败，数据不存在");
        }
        return ActionResult.success("操作成功");
    }

    /**
     * 移除黑名单
     *
     * @param openId 关注者Id
     * @return
     */
    @ApiOperation("移除黑名单")
    @DeleteMapping("/{openId}/Actions/BlackList")
    public ActionResult deleteBatchUnBlack(@PathVariable("openId") String openId) throws WxErrorException {
        boolean flag = mpUserService.deleteBatchUnBlack(openId);
        if (flag == false) {
            return ActionResult.fail("操作失败，数据不存在");
        }
        return ActionResult.success("操作成功");
    }
}
